# Contributor: Oleg A. Khlybov <fougas@mail.ru>

_realname=scalapack
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.1.0
pkgrel=2
arch=('any')
pkgdesc="Scalable Linear Algebra PACKage (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-gcc-fortran" "${MINGW_PACKAGE_PREFIX}-openblas" "${MINGW_PACKAGE_PREFIX}-msmpi")
options=('strip' 'staticlibs')
license=('LGPL')
url="https://www.netlib.org/scalapack"
source=("https://www.netlib.org/${_realname}/${_realname}-${pkgver}.tgz"
        '0001-fix-fortran-mangling.patch'
        '0002-set-proper-language.patch'
        '0003-force-single-lib.patch'
        '0004-fix-pc.patch'
        '0005-disable-testing.patch')
sha256sums=('61d9216cf81d246944720cfce96255878a3f85dec13b9351f1fa0fd6768220a6'
            '24c935293801f64074cf2c749ed54c738b21004bcf6139537d69dee53a19aec6'
            '11e60508f4257d2d8c1e57f016420acbcdec557624d06e63b714f7ada0054638'
            'd44074c56a0562986db2399fcae5cfa9058109e5c524a4fdee2effc09259ea43'
            '0537ac3b058897c53a561a023cdcd74cff65422af06fc9fdf05fa1e3cbecdd3d'
            '23b85b9fb75bbb4c9ba1650319de5be04d7ef6efb86896e25e6af317f738c34c')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  for p in ${source[*]:1}; do
    patch -p1 -i "${srcdir}/$p"
  done
}

build() {
  declare -a _btype
  if check_option "debug" "y"; then
    _btype=Debug
  else
    _btype=Release
  fi
  cd "${srcdir}"
  mkdir -p build-${CARCH}s && cd build-${CARCH}s
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=${_btype} \
      -DBUILD_SHARED_LIBS=OFF \
      -DBUILD_TESTING=OFF \
      ../${_realname}-${pkgver}
  make
  cd "${srcdir}"
  mkdir -p build-${CARCH} && cd build-${CARCH}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=${_btype} \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_NEED_RESPONSE=ON \
      -DBUILD_TESTING=OFF \
      ../${_realname}-${pkgver}
  make
}

package() {
  # FIXME proper cmake configuration
  cd "${srcdir}/build-${CARCH}s"
  make DESTDIR="${pkgdir}" install
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install
  cd "${pkgdir}/${MINGW_PREFIX}" && mv Testing bin
}
